package Q5;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.SocketException;
import Q5.ServerTool;

public class ServerThreadRunnable implements Runnable {
    private String name;
    private int id;

    public ServerThreadRunnable(String name, int id) {
        this.name = name;
        this.id = id;
    }

    @Override
    public void run() {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(TalkServer.sockets[id].getInputStream()));
            while (true) {
                //输入流获取客户端信息
                String msg = br.readLine();
                if (msg == null) {
                    break;
                }
                if ("发送文件".equalsIgnoreCase(msg)) {
                    String fileName = String.valueOf(br.read());
                    ServerTool.receiveFile(id, fileName, name);
                    ServerTool.shareMsg(id, name, "分享文件" + fileName);
                } else if ("下载".equalsIgnoreCase(msg)) {
                    String fileName = br.readLine();
                    File file = new File("D:/Talk/server/");
                    ServerTool.sendFileToClient(id,file,name);
                } else {
                    ServerTool.shareMsg(id, name, msg);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
